package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.crypto.SecretKey;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import javax.swing.JTabbedPane;
import java.awt.GridBagLayout;
import java.io.File;
import java.util.HashMap;
import java.util.StringTokenizer;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JRootPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import code.Language;
import code.Settings;
import javax.swing.JRadioButton;
import code.SymmetricEncDec;

public class OtherEncryptions extends JPanel {
	
	Color c= new Color(0x80, 0x80, 0xFF);
	
	Color TAc = new Color(246, 250, 250);

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JLabel jLabel = null;

	private JTabbedPane jTabbedPane = null;

	private JPanel jPanel = null;

	private JButton jButton = null;

	private JButton jButton1 = null;

	private JButton jButton2 = null;

	private JTextArea jTextArea = null;
	
	ATOCOmain atocoframe;
	
	private HashMap labels;
	
	public int selected;
	
	ButtonGroup rbgroup = new ButtonGroup();  //  @jve:decl-index=0:
	
	ButtonGroup rb2group = new ButtonGroup(); 

	private JRadioButton jRadioButton = null;

	private JRadioButton jRadioButton1 = null;

	private JLabel jLabel1 = null;

	private JLabel jLabel3 = null;

	private JLabel jLabel4 = null;

	private Component jLabel5 = null;

	private Component jLabel6 = null;

	private Component jLabel7 = null;

	private Component jLabel8 = null;

	private Component jLabel9 = null;

	private Component jLabel10 = null;

	private Component jLabel11 = null;

	private Component jLabel12 = null;
	private SecretKey key;
	private String file;
	String padding;
	String nextclass;
	HashMap type= new HashMap();
	private Settings s;
	
//	JScrollPane scroller;

	/**
	 * This is the default constructor
	 */
	public OtherEncryptions(ATOCOmain atocoframe, int selected) {
		super();
		Settings s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "OtherEncryptions", (String) s.global.get("installPath"));
		this.selected = selected;
		initialize();
		this.atocoframe =atocoframe;
	
	}
	public void setKey(SecretKey key){
		this.key=key;
		if(key!=null){
			System.out.println("I got the key");
			System.out.println(key);
			jLabel11.setVisible(true);
		}else{
			jLabel11.setVisible(false);
			
		}
	}


	public void setFile(String file){
		this.file=file;
		if(file!=null){
			jLabel3.setText((String) labels.get("jLabel3")+" "+file);

			jLabel10.setVisible(true);
		}else{
			jLabel3.setText((String) labels.get("jLabel3"));
			jLabel10.setVisible(false);
		}
	}

	public void setPadding(String padding){
		this.padding = padding;
		System.out.println("\n\n\n\n\n"+this.padding);

	}
	private void initialize() {
		this.setSize(852, 451);
		jLabel1 = new JLabel();
		jLabel1.setText((String) labels.get("jLabel1"));
		jLabel1.setBounds(new Rectangle(566, 15, 69, 18));
		jLabel1.setFont(new java.awt.Font("Arial", Font.BOLD, 10));
		jLabel1.setForeground(c);
		jLabel = new JLabel();
		jLabel.setBounds(new Rectangle(32, 25, 300, 26));
		jLabel.setText((String) labels.get("jLabel"));
		this.setLayout(null);
		this.add(jLabel, null);
		this.add(getJTabbedPane(), null);
		this.setBackground(Color.WHITE);
	}

	
	private JTabbedPane getJTabbedPane() {
		if (jTabbedPane == null) {
			jTabbedPane = new JTabbedPane();
			jTabbedPane.setBounds(new Rectangle(25, 70, 800, 450));
			//type.put(0, "Rjindael");
			type.put(0, "Serpent");
			type.put(1, "Blowfish");
			type.put(2, "RC2");
			type.put(3, "RC4");
			type.put(4, "RC5");
			type.put(5, "RC6");
			setPadding((String)type.get(selected));
			jTabbedPane.addTab("Serpent", null, getJPanel(), null);
			
			jTabbedPane.addTab("Blowfish", null);
			jTabbedPane.addTab("RC2", null);
			jTabbedPane.addTab("RC4", null);
			jTabbedPane.addTab("RC5", null);
			jTabbedPane.addTab("RC6", null);
			jTabbedPane.setSelectedIndex(selected);
			jTabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
                public void stateChanged(javax.swing.event.ChangeEvent e) {
                       selected=jTabbedPane.getSelectedIndex();
                       setPadding((String)type.get(selected));
                       String f= null;
                       setFile(f);
                       SecretKey k= null;
                       setKey(k);
                       
                }
        });


		}
		return jTabbedPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel12   = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
			jLabel12.setBounds(new Rectangle(120, 240, 25, 25));
			jLabel12.setVisible(false);
			jLabel11   = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
			jLabel11.setBounds(new Rectangle(80, 140, 25, 25));
			jLabel11.setVisible(false);
			jLabel10   = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/accepticon.gif"));
			jLabel10.setBounds(new Rectangle(80, 30, 25, 25));
			jLabel10.setVisible(false);
			jLabel9   = new JLabel("Encryption");
			jLabel9.setBounds(new Rectangle(50, 240, 70, 25));
			jLabel8   = new JLabel("Key");
			jLabel8.setBounds(new Rectangle(50, 140, 40, 25));
			jLabel7  = new JLabel("File");
			jLabel7.setBounds(new Rectangle(50, 30, 40, 25));
			jLabel5 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			jLabel5.setBounds(new Rectangle(80, 170, 25, 25));
			jLabel5.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jTextArea.setText((String) labels.get("oldkey"));
				}
			});
			jLabel4 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/questionmark.jpg"));
			jLabel4.setBounds(new Rectangle(80, 195, 25, 25));
			jLabel4.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jTextArea.setText((String) labels.get("newkey"));
				}
			});
			jLabel6 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/nextmiddle.gif"));
			jLabel6.setBounds(new Rectangle(280, 170, 50, 50));
			jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if(nextclass != null){
						if(nextclass.equalsIgnoreCase("newkeygui")){
							atocoframe.contentPane(new KeyCreationSymmetric(atocoframe, (String)type.get(selected), passclass(), (String)type.get(selected)));
						}else if(nextclass.equalsIgnoreCase("selectkeygui")){
							atocoframe.contentPane(new SecretKeyList(atocoframe, passclass(), (String)type.get(selected)));
						}
					}

				}
			});
			jLabel3  = new JLabel();
			jLabel3.setBounds(new Rectangle(80, 110, 350, 20));
			jLabel3.setText((String) labels.get("jLabel3"));
			jLabel3.setFont(new Font("Arial", Font.ITALIC, 12));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setBackground(Color.WHITE);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
			jPanel.add(getJTextArea(), null);
			jPanel.add(jLabel1, null);
			jPanel.add(getJRadioButton(), null);
			jPanel.add(getJRadioButton1(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(jLabel4, null);
			jPanel.add(jLabel5, null);
			jPanel.add(jLabel6, null);
			jPanel.add(jLabel7, null);
			jPanel.add(jLabel8, null);
			jPanel.add(jLabel9, null);
			jPanel.add(jLabel10, null);
			jPanel.add(jLabel11, null);
			jPanel.add(jLabel12, null);
		}
		return jPanel;
	}
	

	public OtherEncryptions passclass(){
		return this;
	}
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(80, 270, 100, 40));
			jButton.setText((String) labels.get("jButton"));
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SymmetricEncDec sym= new SymmetricEncDec();
					if((key!=null)&&(file!=null))
					{
						System.out.println("file: "+file+" key: "+key);
						sym.encrypt(key, padding, file);
						jLabel12.setVisible(true);
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(190, 270, 100, 40));
			jButton1.setText((String) labels.get("jButton1"));
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					SymmetricEncDec sym= new SymmetricEncDec();
					if((key!=null)&&(file!=null))
					{
						System.out.println("file: "+file+" key: "+key);
						sym.decrypt(key, padding, file);
						jLabel12.setVisible(true);
					}
				}
			});
		}
		return jButton1;
	}

	public void browse(){
		JFileChooser fc = new JFileChooser(); 
		fc.setDialogTitle((String) labels.get("fc"));
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fc.setMultiSelectionEnabled(true);
		fc.showOpenDialog(this); 

		try { 
			// Open an input stream 
			File f= fc.getSelectedFile();
			//File[] f =fc.getSelectedFiles();
			System.out.println(f);
			if(!f.equals("null")){
				String s=f.getPath();
				setFile(s);

			}

		}catch(Exception e){

		}

	}
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/klasor.gif"));
			jButton2.setText((String) labels.get("jButton2"));
			jButton2.setBounds(new Rectangle(80, 60, 155, 40));
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					System.out.println("actionPerformed()"); // TODO Auto-generated Event stub actionPerformed()
					browse();
				}
			});
		}
		return jButton2;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(460, 42, 273, 277));
			jTextArea.setBackground(TAc);
			jTextArea.setLineWrap(true);
			jTextArea.setWrapStyleWord(true);
			jTextArea.setEditable(false);
			
		}
		return jTextArea;
	}
	private String parseType(String path){
		StringTokenizer st = new StringTokenizer(path, ",");
		String type=new String();
		while(st.hasMoreTokens()){
		type=st.nextToken();
		}
		StringTokenizer s = new StringTokenizer(type, "]");
		if(s.hasMoreTokens()){
			type=s.nextToken();
		}
		return type.trim();
	}


	private JRadioButton getJRadioButton() {
		if (jRadioButton == null) {
			jRadioButton = new JRadioButton();
			jRadioButton.setBounds(new Rectangle(110, 170, 158, 21));
			jRadioButton.setText((String) labels.get("jRadioButton"));
			jRadioButton.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton.isSelected())
					{
						nextclass="newkeygui";
						System.out.println("radiobutton");
					}

				}
			});rb2group.add(jRadioButton);
		}
		return jRadioButton;
	}

	/**
	 * This method initializes jRadioButton4	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButton1() {
		if (jRadioButton1 == null) {
			jRadioButton1 = new JRadioButton();
			jRadioButton1.setBounds(new Rectangle(110, 195, 158, 21));
			jRadioButton1.setText((String) labels.get("jRadioButton1"));
			jRadioButton1.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e){
					if(jRadioButton1.isSelected())
					{
						nextclass="selectkeygui";
						System.out.println("radiobutton4");
					}

				}
			});
			rb2group.add(jRadioButton1);
		}
		return jRadioButton1;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
